package com.mmloo.common.util;

import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by jessy on 2015/8/19.
 */
public class ImageManager {

    /**
     *
     * @param filename 要上传的图片文件名
     * @return 通过检测文件的后缀名来判断要上传的文件的类型。
     */
    public boolean isRequire(String filename){
        if(filename.endsWith(".jpg")||filename.endsWith(".gif")||filename.endsWith(".png"))
        return true;
        else return false;
    }

    /**
     * filename 文件名通过生成器生成
     *  String path=request.getSession().getServletContext().getRealPath("");
     * @param commonsMultipartFiles  支持多文件上传
     * @param path 保存上传文件的文件夹路径
     * 文件上传
     */
    public List<String> upload(CommonsMultipartFile[] commonsMultipartFiles,String path){
        File file;
        String filePath;
        List<String> imageUrls=new ArrayList<>();
        for(CommonsMultipartFile commonsFile:commonsMultipartFiles){
            if(isRequire(commonsFile.getOriginalFilename())){
                String fileName=TimeUtil.getCurrentTimeInString()+".jpg";
                filePath = path + "\\resources\\images\\"+fileName;
                file = new File(filePath);
                try {
                    commonsFile.transferTo(file);
                    imageUrls.add("\\resources\\images\\"+fileName);
                } catch (IllegalStateException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return imageUrls;
    }

    /**
     *
     * @param response httpServletResponse
     * @param filename  要下载的文件的绝对路径
     * 文件下载
     */
    public void downLoad(HttpServletResponse response,String filename){
        File file = new File(filename);
        FileInputStream input = null;
        OutputStream output = null;
        response.setContentType("text/html;charset =utf-8");
        try {
            response.setHeader("Content-Disposition", "attachment;filename="+java.net.URLEncoder.encode(file.getName(), "UTF-8"));
            //解决显示浏览器的下载窗口乱码问题
        } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
        }
        long fileLenght = file.length();
        byte[] buffer = new byte[1024];
        try {
            input = new FileInputStream(file);
            output = response.getOutputStream();
            int len = 0;
            while((len = input.read(buffer,0 , buffer.length))!=-1){
                output.write(buffer, 0, len);
            }

            output.flush();

        } catch (FileNotFoundException e) {

            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        finally{
            try {
                if(output!=null){
                    output.close();
                    output=null;
                }
                if(input!=null)
                    input.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }

    }

}
